package com.example.album;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpSession;

@Controller
public class InteractionHandler {
    @Autowired
    private InteractionService interactionService;
    @Autowired
    private HttpSession session;

    @PostMapping("/getlike")
    @ResponseBody
    public HashMap<String, Object> getLikeStatus(@RequestBody Map<String, String> payload) {
        String albumId = payload.get("albumId");
        String userId = (String) session.getAttribute("userid");
        return interactionService.getLikeStatus(albumId,userId);
    }
    @PostMapping("/changelike")
    @ResponseBody
    public HashMap<String, Object> changeLikeStatus(@RequestBody Map<String, String> payload) {
        String albumId = payload.get("albumId");
        String userId = (String) session.getAttribute("userid");
        System.out.println("changeLikeStatus called for albumId: " + albumId + " and userId: " + userId);
        return interactionService.changeLikeStatus(albumId,userId);
    }

    @PostMapping("/getcollection")
    @ResponseBody
    public HashMap<String, Object> getCollection(@RequestBody Map<String, String> payload) {
        String albumId = payload.get("albumId");
        String userId = (String) session.getAttribute("userid");
        return interactionService.getCollection(albumId,userId);
    }

    @PostMapping("/changecollection")
    @ResponseBody
    public HashMap<String, Object> changeCollection(@RequestBody Map<String, String> payload) {
        String albumId = payload.get("albumId");
        String userId = (String) session.getAttribute("userid");
        System.out.println("changeCollection called for albumId: " + albumId + " and userId: " + userId);
        return interactionService.changeCollection(albumId,userId);
    }


    @PostMapping("/getcomments")
    @ResponseBody
    public HashMap<String, Object> getComments(@RequestBody Map<String, String> payload) {
        String albumId = payload.get("albumId");;
        return interactionService.getComments(albumId);
    }

    @PostMapping("/addcomment")
    @ResponseBody
    public HashMap<String, Object> addComment(@RequestBody Map<String, String> payload) {
        String albumId = payload.get("albumId");
        String userId = (String) session.getAttribute("userid");
        String text = payload.get("text");
        return interactionService.addComment(albumId, userId, text);
    }

    @PostMapping("/deletecomment")
    @ResponseBody
    public HashMap<String, Object> deleteComment(@RequestBody Map<String, String> payload) {
        String commentId = payload.get("commentId");
        return interactionService.deleteComment(commentId);
    }
}